/*
   @Copyright:LibreOJ
   @Author:   tjyemail
   @Problem:  https://loj.ac/problem/104
   @Language: C++
   @Datetime: 20-08-07 14:07
   */
// multi-tree+fread, time 70ms
#include <bits/stdc++.h>
using namespace std;

// 2^(LEVEL+STEP) > 1e7
#define LEVEL 24
#define STEP 3
const int BASE = 1e7+5;
struct Node {
	int size;
	struct Node *child[1<<STEP];
	Node(int s):size(s){memset(child, 0, sizeof(child));}
	~Node() {
		for(int i=1<<STEP; i--;)
			if (child[i]) delete child[i];
	}
} root(0);

inline void insert(Node *p, int x, int l=LEVEL) {
	int i = x>>l;
	p->size++;
	if (p->child[i]==NULL) p->child[i] = new Node(0);
	if (l) insert(p->child[i], x-(i<<l), l-STEP);
	else p->child[i]->size++;
}

inline bool erase(Node *p, int x, int l=LEVEL) {
	if (p==NULL || p->size==0) return false; // not exists
	int i = x>>l, ret = 0;
	if (l) ret = erase(p->child[i], x-(i<<l), l-STEP);
	else if (p->child[i] && p->child[i]->size)
		ret = p->child[i]->size--;
	if (ret) return p->size--;
}

inline int get_rank(Node *p, int x, int l=LEVEL) {
	if (p==NULL || p->size==0) return -1; // not exists
	int i = x>>l, r = 0;
	r = l ? get_rank(p->child[i], x-(i<<l), l-STEP) : 1; // self
	if (r==-1) return -1; // not exists
	for(; i--; r += p->child[i] ? p->child[i]->size : 0);
	return r;
}

inline int get_num(Node *p, int r, int l=LEVEL) {
	for(int i=0; i<(1<<STEP); ++i) {
		if (p->child[i]==NULL || p->child[i]->size==0) continue;
		if (p->child[i]->size<r) r-=p->child[i]->size;
		else return l ? (i<<l) + get_num(p->child[i], r, l-STEP) : i;
	}
	return -1; // not exists
}

// max of [0, i) at level
inline int get_max(Node *p, int i=1<<STEP, int l=LEVEL) {
	if (p==NULL || p->size==0) return -1; // not found
	for(; i--; ){
		if (l) {
			int ret = get_max(p->child[i], 1<<STEP, l-STEP);
			if (ret!=-1) return (i<<l) + ret;
		} else if (p->child[i] && p->child[i]->size) return i;
	}
	return -1; // not found
}

inline int get_prev(Node *p, int x, int l=LEVEL) {
	if (p==NULL || p->size==0) return -1; // not found
	int i = x>>l, ret = -1;
	if (l) ret = get_prev(p->child[i], x-(i<<l), l-STEP);
	return ret!=-1 ? (i<<l) + ret : get_max(p, i, l);
}

// min of [i, 1<<STEP) at level
inline int get_min(Node *p, int i=0, int l=LEVEL) {
	if (p==NULL || p->size==0) return -1; // not found
	for(; i<(1<<STEP); ++i) {
		if (l) {
			int ret = get_min(p->child[i], 0, l-STEP);
			if (ret!=-1) return (i<<l) + ret;
		} else if (p->child[i] && p->child[i]->size) return i;
	}
	return -1; // not found
}

inline int get_next(Node *p, int x, int l=LEVEL) {
	if (p==NULL || p->size==0) return -1; // not found
	int i = x>>l, ret = -1;
	if (l) ret = get_next(p->child[i], x-(i<<l), l-STEP);
	return ret!=-1 ? (i<<l) + ret : get_min(p, i+1, l);
}

typedef int _FR_TYPE;
char _FR_BUF[102400];
struct _FR {
	char *buf, sign;
	size_t size, block, idx, end;
	_FR_TYPE num;
	_FR(char *_buf, size_t _size, size_t _block):buf(_buf), size(_size), block(_block){idx=end=size;}
	inline _FR_TYPE read() {
		if (idx > size - block && end == size) {
			memmove(buf, buf + idx, end - idx);
			end = size - idx + fread(buf + size - idx, 1, idx, stdin);
			idx = 0;
		}
		for (num=0; isspace(buf[idx]); ++idx);
		sign = buf[idx]!='-'?'0':buf[idx++];
		for (; isdigit(buf[idx]); ++idx)
			num = num * 10 + buf[idx] - '0';
		return sign!='-'?num:-num;
	}
} fr(_FR_BUF, sizeof(_FR_BUF), 16);

int main()
{
	int n, o, x;
	Node *p = &root;
	for(n=fr.read(); n--; ){
		o = fr.read(), x = fr.read();
		switch(o) {
			case 1:insert(p, x+BASE);break;
			case 2:erase(p, x+BASE);break;
			case 3:printf("%d\n", get_rank(p, x+BASE));break;
			case 4:printf("%d\n", get_num(p, x)-BASE);break;
			case 5:printf("%d\n", get_prev(p, x+BASE)-BASE);break;
			case 6:printf("%d\n", get_next(p, x+BASE)-BASE);break;
		}
	}
	return 0;
}
